package com.thinkgem.jeesite.modules.ws;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.ws.AppAuthEnum;
import com.thinkgem.jeesite.common.ws.CallPhoneResult;
import com.thinkgem.jeesite.common.ws.ReturnResult;
import com.thinkgem.jeesite.modules.ship.entity.ShipIllegal;
import com.thinkgem.jeesite.modules.ship.entity.ShipIllegalReport;
import com.thinkgem.jeesite.modules.ship.entity.ShipSeaBook;
import com.thinkgem.jeesite.modules.ship.service.ShipIllegalReportService;
import com.thinkgem.jeesite.modules.ship.service.ShipIllegalService;
import com.thinkgem.jeesite.modules.ship.service.ShipSeaBookService;
import com.thinkgem.jeesite.modules.sys.entity.Area;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.AreaService;
import com.thinkgem.jeesite.common.persistence.Page;

/**
 * TODO:船舶管理webServices
 *
 * @author 作者：lgx
 * @date 创建时间：2016年4月17日
 * @version V1.0
 */
@Path("/shipWs")
@Component
public class ShipWs {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(ShipWs.class);

	@Autowired
	private ShipIllegalService shipIllegalService;
	@Autowired
	private ShipIllegalReportService shipIllegalReportService;
	@Autowired
	private ShipSeaBookService shipSeaBookService;
	@Autowired
	private AreaService areaService;
	/**
	 * 船舶违法信息查询
	 * 
	 * @param sso 
	 * 			验证信息
	 * @param pageSize
	 *            每页显示条数
	 * @param pageIndex
	 *            第几页
	 * @param where
	 *        查询条件
	 *  @param orderby
	 *        排序字段
	 * @return 船舶违法信息列表（json）
	 * @author lgx Date:2016年4月17日；
	 */
	@GET
	@Path("/getShipIllegalList")
	@Produces(MediaType.APPLICATION_JSON)
	public CallPhoneResult getShipIllegalList(@QueryParam("sso") String sso,@QueryParam("pageSize") int pageSize,@QueryParam("pageIndex") int pageIndex,
			@QueryParam("where") String where,@QueryParam("orderby") String orderby) 
	{
		try
		{
			Map<String, Object> mapJsonSso  = (Map<String, Object>)JsonMapper.fromJsonString(sso,Map.class);
			if(mapJsonSso.get("token")!=null)
			{
				ShipIllegal shipIllegal = new ShipIllegal();
				if(StringUtils.isNotEmpty(where.trim()))
				{
					shipIllegal = (ShipIllegal)JsonMapper.fromJsonString(where.trim(),ShipIllegal.class);
				}
				ReturnResult returnResult = new ReturnResult();
				
				Page<ShipIllegal> page = new Page<ShipIllegal>();
				page.setPageSize(pageSize);
				page.setPageNo(pageIndex);
				page.setOrderBy(orderby);
				shipIllegal.setPage(page);
				returnResult.setArrayList(shipIllegalService.findList(shipIllegal));
				Map<String,Object> dataPagination = new HashMap<String, Object>();
				dataPagination.put("totalRow", shipIllegal.getPage().getCount());
				dataPagination.put("pageSize", pageSize);
				dataPagination.put("pageIndex", pageIndex);
				returnResult.setDataPagination(dataPagination);
				return new CallPhoneResult(String.valueOf(AppAuthEnum.success.ordinal()),String.valueOf(AppAuthEnum.success.ordinal()),returnResult);
			}
			else
			{
				return new CallPhoneResult(String.valueOf(AppAuthEnum.failfull.ordinal()));
			}
		}
		catch(Exception e)
		{
			return new CallPhoneResult(String.valueOf(AppAuthEnum.exception.ordinal()),String.valueOf(AppAuthEnum.exception.ordinal()));
		}
	}
	
	/**
	 * 查询某个区域的下级区域
	 * 
	 * @param sso 
	 * 			验证信息
	 * @param pageSize
	 *            每页显示条数
	 * @param pageIndex
	 *            第几页
	 * @param where
	 *        查询条件
	 *  @param orderby
	 *        排序字段
	 * @return 查询某个区域的下级区域列表（json）
	 * @author lgx Date:2016年4月17日；
	 */
	@GET
	@Path("/queryAreaChilds")
	@Produces(MediaType.APPLICATION_JSON)
	public CallPhoneResult queryAreaChilds(@QueryParam("sso") String sso,@QueryParam("pageSize") int pageSize,@QueryParam("pageIndex") int pageIndex,
			@QueryParam("where") String where,@QueryParam("orderby") String orderby) 
	{
		try
		{
			Map<String, Object> mapJsonSso  = (Map<String, Object>)JsonMapper.fromJsonString(sso,Map.class);
			if(mapJsonSso.get("token")!=null)
			{
				Area area = new Area();
				if(StringUtils.isNotEmpty(where.trim()))
				{
					area = (Area)JsonMapper.fromJsonString(where.trim(),Area.class);
				}
				area = areaService.findAreaByCode(area);
				ReturnResult returnResult = new ReturnResult();
				
				Page<Area> page = new Page<Area>();
				page.setPageSize(pageSize);
				page.setPageNo(pageIndex);
				page.setOrderBy(orderby);
				area.setPage(page);
				
				returnResult.setArrayList(areaService.queryAreaByParentId(area));
				Map<String,Object> dataPagination = new HashMap<String, Object>();
				dataPagination.put("totalRow", area.getPage().getCount());
				dataPagination.put("pageSize", pageSize);
				dataPagination.put("pageIndex", pageIndex);
				returnResult.setDataPagination(dataPagination);
				return new CallPhoneResult(String.valueOf(AppAuthEnum.success.ordinal()),String.valueOf(AppAuthEnum.success.ordinal()),returnResult);
			}
			else
			{
				return new CallPhoneResult(String.valueOf(AppAuthEnum.failfull.ordinal()));
			}
		}
		catch(Exception e)
		{
			return new CallPhoneResult(String.valueOf(AppAuthEnum.exception.ordinal()),String.valueOf(AppAuthEnum.exception.ordinal()));
		}
	}
	/**
	 * 保存违法举报信息
	 * 
	 * @param sso 
	 * 			验证信息
	 * @param json
	 *            举报对象信息
	 * @return 违法举报信息（json）
	 * @author lgx Date:2016年4月17日；
	 */
	@GET
	@Path("/saveShipIllegalReport")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public CallPhoneResult saveShipIllegalReport(@QueryParam("sso") String sso,@QueryParam("json") String json) {
		try
		{
			Map<String, Object> mapJsonSso  = (Map<String, Object>)JsonMapper.fromJsonString(sso,Map.class);
			if(mapJsonSso.get("token")!=null)
			{
				ShipIllegalReport shipIllegalReport = (ShipIllegalReport)JsonMapper.fromJsonString(json,ShipIllegalReport.class);
				
				Map<String, Object> error = new HashMap<String, Object>();
				if(StringUtils.isBlank(shipIllegalReport.getShipNo()))
				{
					error.put("shipNo", "ShipNo必填值为空");
				}
				if(StringUtils.isBlank(shipIllegalReport.getAddress()))
				{
					error.put("address", "Address必填值为空");
				}
				if(StringUtils.isBlank(shipIllegalReport.getCity()))
				{
					error.put("city", "city必填值为空");
				}
				if(StringUtils.isBlank(shipIllegalReport.getReportUserId()))
				{
					error.put("reportUserId", "reportUserId必填值为空");
				}
				if(StringUtils.isBlank(shipIllegalReport.getReportUsername()))
				{
					error.put("reportUsername", "reportUsername必填值为空");
				}
				if(StringUtils.isBlank(shipIllegalReport.getUnlawfulAct()))
				{
					error.put("unlawfulAct", "unlawfulAct必填值为空");
				}
				
				if(error.isEmpty())
				{
					ShipIllegalReport shipIllegalReportNew = new ShipIllegalReport();
					BeanUtils.copyProperties(shipIllegalReport, shipIllegalReportNew);
					shipIllegalReportNew.setId(IdGen.uuid());
					User user = new User();
					user.setId(shipIllegalReport.getReportUserId());
					user.setName(shipIllegalReport.getReportUsername());
					shipIllegalReportNew.setCreateBy(user);
					shipIllegalReportNew.setUpdateBy(user);
					if(shipIllegalReportService.getMulti(shipIllegalReportNew).isEmpty())
						shipIllegalReportService.save(shipIllegalReportNew);
					return new CallPhoneResult(String.valueOf(AppAuthEnum.success.ordinal()),String.valueOf(AppAuthEnum.success.ordinal()),shipIllegalReportNew);
				}
				else
				{
					return new CallPhoneResult(String.valueOf(AppAuthEnum.success.ordinal()),String.valueOf(AppAuthEnum.failfull.ordinal()),error);
				}
			}
			else
			{
				return new CallPhoneResult(String.valueOf(AppAuthEnum.failfull.ordinal()));
			}
		}
		catch(Exception e)
		{
			return new CallPhoneResult(String.valueOf(AppAuthEnum.exception.ordinal()),String.valueOf(AppAuthEnum.exception.ordinal()));
		}
	}
	
	/**
	 * 申领出海边防证件预约查询
	 * 
	 * @param sso
	 *            单点验证信息
	 * @param pageSize
	 *            每页显示多少条
	 * @param pageIndex
	 * 			 第几页
	 * @param where
	 *          查询条件
	 * @param orderby
	 *          排序字段
	 * @return 申领出海边防证件预约列表（json）
	 * @author lgx Date:2016年4月17日；
	 */
	@GET
	@Path("/findShipSeaBook")
	@Produces(MediaType.APPLICATION_JSON)
	public CallPhoneResult findShipSeaBook(@QueryParam("sso") String sso,@QueryParam("pageSize") int pageSize,@QueryParam("pageIndex") int pageIndex,
			@QueryParam("where") String where,@QueryParam("orderby") String orderby) 
	{
		try
		{
			Map<String, Object> mapJsonSso  = (Map<String, Object>)JsonMapper.fromJsonString(sso,Map.class);
			if(mapJsonSso.get("token")!=null)
			{
				ShipSeaBook shipSeaBook = new ShipSeaBook();
				if(StringUtils.isNotEmpty(where.trim()))
				{
					shipSeaBook = (ShipSeaBook)JsonMapper.fromJsonString(where.trim(),ShipSeaBook.class);
				}
				ReturnResult returnResult = new ReturnResult();
				Page<ShipSeaBook> page = new Page<ShipSeaBook>();
				page.setPageSize(pageSize);
				page.setPageNo(pageIndex);
				page.setOrderBy(orderby);
				shipSeaBook.setPage(page);
				returnResult.setArrayList(shipSeaBookService.findList(shipSeaBook));
				Map<String,Object> dataPagination = new HashMap<String, Object>();
				dataPagination.put("totalRow", shipSeaBook.getPage().getCount());
				dataPagination.put("pageSize", pageSize);
				dataPagination.put("pageIndex", pageIndex);
				returnResult.setDataPagination(dataPagination);
				return new CallPhoneResult(String.valueOf(AppAuthEnum.success.ordinal()),String.valueOf(AppAuthEnum.success.ordinal()),returnResult);
			}
			else
			{
				return new CallPhoneResult(String.valueOf(AppAuthEnum.failfull.ordinal()));
			}
		}
		catch(Exception e)
		{
			return new CallPhoneResult(String.valueOf(AppAuthEnum.exception.ordinal()),String.valueOf(AppAuthEnum.exception.ordinal()));
		}
	}
	/**
	 * 保存预约信息
	 * 
	 * @param sso 
	 * 			验证信息
	 * @param json
	 *            预约对象信息
	 * @return 预约信息（json）
	 * @author lgx Date:2016年4月17日；
	 */
	@GET
	@Path("/saveShipSeaBook")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public CallPhoneResult saveShipSeaBook(@QueryParam("sso") String sso,@QueryParam("json") String json) {
		try
		{
			Map<String, Object> mapJsonSso  = (Map<String, Object>)JsonMapper.fromJsonString(sso,Map.class);
			if(mapJsonSso.get("token")!=null)
			{
				ShipSeaBook shipSeaBoook = (ShipSeaBook)JsonMapper.fromJsonString(json,ShipSeaBook.class);
				
				Map<String, Object> error = new HashMap<String, Object>();
				if(StringUtils.isBlank(shipSeaBoook.getShipNo()))
				{
					error.put("shipNo", "ShipNo必填值为空");
				}
				if(null ==shipSeaBoook.getAppointDate())
				{
					error.put("appointDate", "AppointDate必填值为空");
				}
				if(StringUtils.isBlank(shipSeaBoook.getHomePort()))
				{
					error.put("homePort", "homePort必填值为空");
				}
				if(StringUtils.isBlank(shipSeaBoook.getAppointAddress()))
				{
					error.put("appointAddress", "appointAddress必填值为空");
				}
				if(StringUtils.isBlank(shipSeaBoook.getBookUserId()))
				{
					error.put("bookUserId", "bookUserId必填值为空");
				}
				if(StringUtils.isBlank(shipSeaBoook.getBookUserName()))
				{
					error.put("bookUserName", "bookUserName必填值为空");
				}
				if(StringUtils.isBlank(shipSeaBoook.getTelephone()))
				{
					error.put("telephone", "telephone必填值为空");
				}
				if(error.isEmpty())
				{
					ShipSeaBook shipSeaBook = new ShipSeaBook();
					BeanUtils.copyProperties(shipSeaBoook, shipSeaBook);
					shipSeaBook.setId(IdGen.uuid());
					User user = new User();
					user.setId(shipSeaBoook.getBookUserId());
					user.setName(shipSeaBoook.getBookUserName());
					shipSeaBook.setCreateBy(user);
					shipSeaBook.setUpdateBy(user);
					if(shipSeaBookService.getMulti(shipSeaBook).isEmpty())
					shipSeaBookService.save(shipSeaBook);
					return new CallPhoneResult(String.valueOf(AppAuthEnum.success.ordinal()),String.valueOf(AppAuthEnum.success.ordinal()),shipSeaBook);
				}
				else
				{
					return new CallPhoneResult(String.valueOf(AppAuthEnum.success.ordinal()),String.valueOf(AppAuthEnum.failfull.ordinal()),error);
				}
			}
			else
			{
				return new CallPhoneResult(String.valueOf(AppAuthEnum.failfull.ordinal()));
			}
		}
		catch(Exception e)
		{
			return new CallPhoneResult(String.valueOf(AppAuthEnum.exception.ordinal()),String.valueOf(AppAuthEnum.exception.ordinal()));
		}
	}

}
